package com.autumn.chat.core;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.autumn.chat.constant.BLiveOperationConstant;
import com.autumn.chat.http.DoBLiveApi;
import com.autumn.chat.socket.SSLWebSocketClient;
import com.autumn.chat.struct.blive.BLiveProto;
import com.autumn.chat.task.ProjectHearBeatTask;
import com.autumn.chat.task.WSSHeartBeatTask;
import com.googlecode.lanterna.TerminalSize;
import com.googlecode.lanterna.TextColor;
import com.googlecode.lanterna.gui2.*;
import com.googlecode.lanterna.screen.Screen;
import com.googlecode.lanterna.screen.TerminalScreen;
import com.googlecode.lanterna.terminal.DefaultTerminalFactory;
import com.googlecode.lanterna.terminal.Terminal;
import com.googlecode.lanterna.terminal.TerminalFactory;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.enums.ReadyState;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.audio.AudioPlayer;

import javax.sound.sampled.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;


public class Demo {

    private static final Logger log = LoggerFactory.getLogger(Demo.class);

    public static void test(){
        System.out.println("----测试方法开始----");

        try {
//            new Demo().tm();
            // 定义动画帧
            String[] frames = new String[] {
                    "⠁", "⠂", "⠄", "⡀", "⢀", "⠠", "⠐", "⠈",
                    "⠁", "⠂", "⠄", "⡀", "⢀", "⠠", "⠐", "⠈",
                    "⠁", "⠂", "⠄", "⡀", "⢀", "⠠", "⠐", "⠈",
                    "⠁", "⠂", "⠄", "⡀", "⢀", "⠠", "⠐", "⠈",
                    "⠁", "⠂", "⠄", "⡀", "⢀", "⠠", "⠐", "⠈",
                    "⠁", "⠂", "⠄", "⡀", "⢀", "⠠", "⠐", "⠈",
                    "⠁", "⠂", "⠄", "⡀", "⢀", "⠠", "⠐", "⠈",
                    "⠁", "⠂", "⠄", "⡀", "⢀", "⠠", "⠐", "⠈",
                    "⠁", "⠂", "⠄", "⡀", "⢀", "⠠", "⠐", "⠈",
                    "⠁", "⠂", "⠄", "⡀", "⢀", "⠠", "⠐", "⠈",
                    "⠁", "⠂", "⠄", "⡀", "⢀", "⠠", "⠐", "⠈",
                    "⠁", "⠂", "⠄", "⡀", "⢀", "⠠", "⠐", "⠈",
                    "⠁", "⠂", "⠄", "⡀", "⢀", "⠠", "⠐", "⠈"
            };

            // 无限循环动画
            int i = 0;
            while (true) {
                // 输出当前帧
                System.out.print("\r" + frames[i]);
//                System.out.flush();

                // 休眠一定时间，切换到下一帧
                Thread.sleep(100);
                i = (i + 1) % frames.length;
            }










        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("----测试方法结束----");


    }

    private void tm() throws Exception{

        // Setup terminal and screen layers
        Terminal terminal = new DefaultTerminalFactory().createTerminal();
        Screen screen = new TerminalScreen(terminal);
        screen.startScreen();

        // Create panel to hold components
        Panel panel = new Panel();
        panel.setLayoutManager(new GridLayout(2));

        panel.addComponent(new Label("Forename"));
        panel.addComponent(new TextBox());

        panel.addComponent(new Label("Surname"));
        panel.addComponent(new TextBox());

        panel.addComponent(new EmptySpace(new TerminalSize(0,0))); // Empty space underneath labels
        panel.addComponent(new Button("Submit"));

        // Create window to hold the panel
        BasicWindow window = new BasicWindow();
        window.setComponent(panel);

        // Create gui and start gui
        MultiWindowTextGUI gui = new MultiWindowTextGUI(screen, new DefaultWindowManager(), new EmptySpace(TextColor.ANSI.BLACK));
        gui.addWindowAndWait(window);




        log.info("12313213213");

    }

    private static void readWav() {
        try {
            // 指定音频文件路径
            String filePath = "D:\\temp\\Chimes.wav";

            // 获取音频输入流
            AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(filePath).getAbsoluteFile());

            // 获取音频格式
            AudioFormat audioFormat = audioInputStream.getFormat();

            // 转换音频格式
            DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
            SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem.getLine(info);
            sourceDataLine.open(audioFormat);
            sourceDataLine.start();

            // 读取音频数据并播放
            byte[] audioData = new byte[4096];
            int bytesRead = 0;
            while (bytesRead != -1) {
                bytesRead = audioInputStream.read(audioData, 0, audioData.length);
                if (bytesRead >= 0) {
                    sourceDataLine.write(audioData, 0, bytesRead);
                }
            }

            // 关闭流
            sourceDataLine.drain();
            sourceDataLine.close();
            audioInputStream.close();
        } catch (Exception ex) {
            System.out.println("Error playing audio file: " + ex.getMessage());
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {
        test();
    }
}
